package com.lcq.gulimall.member.controller;

import java.util.Arrays;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.lcq.common.constant.AuthConstant;
import com.lcq.common.exception.BizCodeEnum;
import com.lcq.common.exception.PhoneExistException;
import com.lcq.common.exception.UserNameExistException;
import com.lcq.common.vo.MemberRsepVo;
import com.lcq.common.vo.UserShowInfoVo;
import com.lcq.gulimall.member.feign.CouponFeign;
import com.lcq.gulimall.member.vo.LoginVo;
import com.lcq.gulimall.member.vo.RegisterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lcq.gulimall.member.entity.MemberEntity;
import com.lcq.gulimall.member.service.MemberService;
import com.lcq.common.utils.PageUtils;
import com.lcq.common.utils.R;

import javax.servlet.http.HttpSession;


/**
 * 会员
 *
 * @author lichuanqi
 * @email lichuanqi0086@163.com
 * @date 2021-02-21 17:56:28
 */
@RestController
@RefreshScope
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @Autowired
    private CouponFeign couponFeign;
//    @Value("${member.name}")
//    private String name;
    /**
     * 列表
     */
//    @RequestMapping("/test")
//    public R test(){
//        return R.ok().put("name",name);
//    }
    @RequestMapping("/coupon")
    public R coupon(@RequestParam Map<String, Object> params){
        R r = couponFeign.list(params);
        return R.ok().put("coupon",r.get("page"));
    }
    @RequestMapping("/list")
    //@RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 注册
     */
    @RequestMapping("/register")
    //@RequiresPermissions("member:member:save")
    public R register(@RequestBody RegisterVo registerVo){
//        try {
            memberService.register(registerVo);
//        }catch (UserNameExistException e){
//            return R.error(BizCodeEnum.MEMBER_USERNAME_EXIST_EXCEPTION.getCode(),BizCodeEnum.MEMBER_USERNAME_EXIST_EXCEPTION.getMsg());
//        }catch (PhoneExistException e){
//            return R.error(BizCodeEnum.MEMBER_PHONE_EXIST_EXCEPTION.getCode(),BizCodeEnum.MEMBER_PHONE_EXIST_EXCEPTION.getMsg());
//        }catch (Exception e){
//            return R.error(BizCodeEnum.UNKNOW_EXEPTION.getCode(),BizCodeEnum.UNKNOW_EXEPTION.getMsg());
//        }
        return R.ok();
    }

    /**
     * 登录
     */
    @RequestMapping("/login")
    //@RequiresPermissions("member:member:save")
    public R login(@RequestBody LoginVo loginVo){
        MemberRsepVo user = memberService.login(loginVo);
        //String json = JSON.toJSONString(user, SerializerFeature.WriteClassName);
        return R.ok().putObject(AuthConstant.LOGIN_USER,user);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }
}
